package com.braydeejohnson.untitled;

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;

public class Square {
	private FloatBuffer vertexBuffer;
	private FloatBuffer textureBuffer;
	private FloatBuffer interleavedBuffer;
	private FloatBuffer colorBuffer;
	private ShortBuffer indexBuffer;
	private Random random = new Random();
	private static final String TAG = Square.class.getSimpleName();
	float xt,x,y,depth;

	private float vertices[] = {
			1.0f, 1.0f, 0.0f, //bottom left
			1.0f, 64.0f, 0.0f,// top left
			64.0f, 1.0f, 0.0f,//bottom right
			64.0f, 64.0f, 0.0f//top right
	};
	private float[] colors = {
			0.65f, 0.75f, 0.65f, 1f,
			0.55f, 0.75f, 0.55f, 1f,
			0.5f, 0.65f, 0.5f, 1f,
			0.5f, 0.65f, 0.5f, 1f
	};
	private short[] indicies = {1,2,0,1,3,2};
	private float texture[] = {
			0.0f, 1.0f,//top left
			0.0f, 0.0f,//bottom left
			1.0f, 1.0f,//top right
			1.0f, 0.0f//bottom right
	};
	private float iva[]= {
			0.0f, 0.0f, 0.0f, //bottom left
			0.0f, 1.0f,//top left tex
			0.0f, 20.0f, 0.0f,// top left
			0.0f, 0.0f,//bottom left tex
			20.0f, 0.0f, 0.0f,//bottom right
			1.0f, 1.0f,//top right tex
			20.0f, 20.0f, 0.0f,//top right
			1.0f, 0.0f//bottom right tex
	};
	private int[] textures = new int[1];
	public Square(int x, int  y) {
		this.x=x;
		this.y=y;
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(((vertices.length)*4));
		byteBuffer.order(ByteOrder.nativeOrder());
		interleavedBuffer = byteBuffer.asFloatBuffer();
		interleavedBuffer.put(vertices);
		//interleavedBuffer.put(texture);
		interleavedBuffer.position(0);
		
		textureBuffer = interleavedBuffer.duplicate();
		textureBuffer.position(3);		
		
		byteBuffer = ByteBuffer.allocateDirect((indicies.length)*2);
		byteBuffer.order(ByteOrder.nativeOrder());
		indexBuffer = byteBuffer.asShortBuffer();
		indexBuffer.put(indicies);
		indexBuffer.position(0);
		
		byteBuffer = ByteBuffer.allocateDirect((colors.length)*4);
		byteBuffer.order(ByteOrder.nativeOrder());
		colorBuffer = byteBuffer.asFloatBuffer();
		colorBuffer.put(colors);
		colorBuffer.position(0);
	}

	
	public void loadGLTexture(GL10 gl, Context context) {
		
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.android);
		gl.glGenTextures(1,textures,0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D,textures[0]);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0 ,bitmap,0);
		bitmap.recycle();
	}
	public void draw(GL10 gl) {
	}
	public void draw2(GL10 gl) {
		
		//gl.glTranslatef((float)x*32,(float)y*32, 0.0f);
		gl.glPushMatrix();
		
		gl.glTranslatef((float)x*64,(float)y*64, 0.0f);
		gl.glFrontFace(GL10.GL_CW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);
		if(false){
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 20, textureBuffer);
		
		}
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, interleavedBuffer);
		//gl.glColor4f(random.nextFloat(), random.nextFloat(), random.nextFloat(), 1.0f);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
		
		//gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, (iva.length)/5);
		gl.glDrawElements(GL10.GL_TRIANGLES, indicies.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		if(false){
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		}
		gl.glDisable(GL10.GL_CULL_FACE);
		gl.glPopMatrix();
	}


	public float getX() {
		return x;
	}


	public void setX(float x) {
		this.x = x;
	}


	public float getY() {
		return y;
	}


	public void setY(float y) {
		this.y = y;
	}
	public float[] getVertices(){
		return vertices;
	}
	public float[] getColors(){
		return colors;
	}
	public short[] getIndicies(){
		return indicies;
	}

}
